layui.define(['table', 'form'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form;

  var placeId = $('input[name="placeId"]').val();
    var userId = $('input[name="userId"]').val();

    table.render({
        elem: '#LAY-table'
        ,url: layui.setter.webRoot + '/place/user/business_type/list.data'
        ,where:{placeId:placeId,userId:userId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'businessType', title: '业务类型'}
            ,{field: 'businessTypeName', title: '业务类型名称'}
            ,{field: 'incomeRatio', title: '分成比例'}
            ,{title: '操作', width: 140, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
        ]]
        ,page: false
        ,height: 'full'
        ,text: '对不起，加载出现异常！'
    });
  
  //事件
  var active = {
      add: function(){
          layer.open({
              type: 2
              ,title: '新增用户'
              ,content: layui.setter.webRoot + '/place/user/business_type/add.html?placeId=' + placeId+'&userId='+ userId
              ,maxmin: false
              ,area: ['400px', '400px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field; //获取提交的字段
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/place/user/business_type/add.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  layer.close(index);
                                  table.reload('LAY-table-user');
                                  layer.msg('新增成功',{icon: 1});
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,update: function(obj){
          layer.open({
              type: 2
              ,title: '修改设备分成信息'
              ,content: layui.setter.webRoot + '/place/user/business_type/update.html?placeId=' +placeId+'&businessType='+obj.data.businessType+'&userId='+ userId
              ,maxmin: false
              ,area: ['400px', '400px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field; //获取提交的字段
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/place/user/business_type/update.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  layer.close(index);
                                  table.reload('LAY-table');
                                  layer.msg('保存成功',{icon: 1});
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,del:function(obj){
          layer.confirm('确定要删除该业务分成信息吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/place/user/business_type/del.do',
                  data:{placeId:placeId,businessType:obj.data.businessType,userId:userId},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          obj.del();
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
  }

  $('.layui-btn.LAY-btn-fun').on('click', function () {
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
  });

    table.on('tool(LAY-table)', function (obj) {
        var type = obj.event;
        active[type] ? active[type].call(this, obj) : '';
    });
  
  exports('place_user_info', {})
});